package com.wxx.message;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.wxx.bean.ProBean;
import com.wxx.utils.IPUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author: wxx
 * @date: 2024-08-16 17:13
 * @description todo 描述信息
 */
@Slf4j
public class NotionSender extends MessageSenderAbs {

    private String blockId;

    private String secret;

    @Override
    public void init(ProBean proBean) {
        this.blockId = proBean.getProperties().getProperty("notion.blockId");
        this.secret = proBean.getProperties().getProperty("notion.secret");
    }

    @Override
    public String getMsg() {
        try {
            List<String> ipList = IPUtil.getIpList();
            log.info("ipList = " + ipList);
            String bodyTemp = String.format("{\n" +
                    "  \"children\": [\n" +
                    "    {\n" +
                    "      \"object\": \"block\",\n" +
                    "      \"id\": \"fe8efa87-e8d8-4518-a262-5e13dc342204\",\n" +
                    "      \"type\": \"paragraph\",\n" +
                    "      \"paragraph\": {\n" +
                    "        \"rich_text\": [\n" +
                    "          {\n" +
                    "            \"type\": \"text\",\n" +
                    "            \"text\": {\n" +
                    "              \"content\": \"%s\",\n" +
                    "              \"link\": null\n" +
                    "            }\n" +
                    "          }\n" +
                    "        ],\n" +
                    "        \"color\": \"default\"\n" +
                    "      }\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}", instance.format(System.currentTimeMillis()) + "-->" + ipList.toString());
            return bodyTemp;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean sendMessage(String content) {
        HttpRequest patch = HttpRequest.patch(String.format("https://api.notion.com/v1/blocks/%s/children", blockId));
        patch.header("Authorization", String.format("Bearer %s", secret));
        patch.header("Content-Type", "application/json");
        patch.header("Notion-Version", "2022-06-28");
        patch.body(content);
        HttpResponse execute = patch.execute();
        log.info(execute.toString());
        return true;
    }
}
